package com.base.cn.platform.os.service.statistics.pvuv;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.dao.statistics.pvuv.ZStatisticsPageViewCountDao;
import com.base.cn.platform.os.entity.statistics.base.StatisticsDateQueryCondition;
import com.base.cn.platform.os.entity.statistics.pvuv.ZStatisticsPageViewCount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * PV数据统计Biz
 *
 * @author s.li
 * @date 2019/04/17
 */
@Service
public class ZStatisticsPageViewCountBiz extends BaseBiz<ZStatisticsPageViewCount, ZStatisticsPageViewCountDao> {

    @Autowired
    private ZStatisticsPageViewCountDao zStatisticsPageViewCountDao;

    /**
     * 查询渠道分布PV分布
     * @param dateQueryCondition
     * @return ZStatisticsPageViewCount
     */
    public ZStatisticsPageViewCount queryPageViewCountSpread(StatisticsDateQueryCondition dateQueryCondition){
        dateQueryCondition.setTimeCondition(new Date());
        List<Date> dateList = dateQueryCondition.getDayList();
        String timeSql = StatisticsDateQueryCondition.getTimeSql(dateList,"yyyy-MM-dd");
        List<ZStatisticsPageViewCount> zStatisticsPageViewCountList = zStatisticsPageViewCountDao.queryPageViewCountSpread(ZStatisticsPageViewCount.class,timeSql);
        ZStatisticsPageViewCount zStatisticsPageViewCount = new ZStatisticsPageViewCount();
        if(ObjectUtils.isNotEmpty(zStatisticsPageViewCountList)){
            zStatisticsPageViewCount.setAppletCount(zStatisticsPageViewCountList.stream().filter(e->e!=null).mapToInt(ZStatisticsPageViewCount::getAppletCount).sum());
            zStatisticsPageViewCount.setPcCount(zStatisticsPageViewCountList.stream().filter(e->e!=null).mapToInt(ZStatisticsPageViewCount::getPcCount).sum());
            zStatisticsPageViewCount.setIosCount(zStatisticsPageViewCountList.stream().filter(e->e!=null).mapToInt(ZStatisticsPageViewCount::getIosCount).sum());
            zStatisticsPageViewCount.setAnCount(zStatisticsPageViewCountList.stream().filter(e->e!=null).mapToInt(ZStatisticsPageViewCount::getAnCount).sum());
            zStatisticsPageViewCount.setMobileCount(zStatisticsPageViewCountList.stream().filter(e->e!=null).mapToInt(ZStatisticsPageViewCount::getMobileCount).sum());
        }
        return zStatisticsPageViewCount;
    }

    /**
     * 添加PV统计数据
     * @param zStatisticsPageViewCountList
     */
    @Transactional
    public synchronized void createPageViewCount(List<ZStatisticsPageViewCount> zStatisticsPageViewCountList){
        if(ObjectUtils.isNotEmpty(zStatisticsPageViewCountList)){
            String timeWhereSql = zStatisticsPageViewCountList.stream().map(e-> "DATE_FORMAT(requestTime,'%Y-%m-%d %H')='"+DateUtils.format(e.getRequestTime(),"yyyy-MM-dd HH")+"'").collect(Collectors.joining(" or "," 1=1 and (",")"));
            List<ZStatisticsPageViewCount> pageViewCountList = this.find(timeWhereSql,null,null);
            List<ZStatisticsPageViewCount> updateList = new ArrayList<>();
            List<ZStatisticsPageViewCount> addList = new ArrayList<>();
            if(ObjectUtils.isNotEmpty(pageViewCountList)){
                Map<String,ZStatisticsPageViewCount> oldPageViewCountMap = pageViewCountList.stream().collect(Collectors.toMap(e->DateUtils.format(e.getRequestTime(),"yyyy-MM-dd HH"), e->e));
                Map<String,ZStatisticsPageViewCount> newPageViewCountMap = zStatisticsPageViewCountList.stream().collect(Collectors.toMap(e->DateUtils.format(e.getRequestTime(),"yyyy-MM-dd HH"), e->e));
                newPageViewCountMap.forEach((k,v)->{
                    ZStatisticsPageViewCount oldData = oldPageViewCountMap.get(k);
                    if(ObjectUtils.isNotEmpty(oldData)){
                        oldData.setAnCount(v.getAnCount());
                        oldData.setAppletCount(v.getAppletCount());
                        oldData.setIosCount(v.getIosCount());
                        oldData.setMobileCount(v.getMobileCount());
                        oldData.setPcCount(v.getPcCount());
                        updateList.add(oldData);
                    }else{
                        addList.add(v);
                    }
                });
            }else{
                addList.addAll(zStatisticsPageViewCountList);
            }
            if(ObjectUtils.isNotEmpty(addList)){
                this.batchSave(addList);
            }
            if(ObjectUtils.isNotEmpty(updateList)){
                this.updateBatch(updateList);
            }
        }
    }

    /**
     * 查询平台PV数据（以天/小时方式）
     * @param type
     * @param timeSql
     * @return List<ZStatisticsPageViewCount>
     */
    public List<ZStatisticsPageViewCount> queryPageViewCountList(int type,String timeSql){
        Map<String,Object> params = new HashMap<>();
        params.put("type",type);
        params.put("timeSql",timeSql);
        return zStatisticsPageViewCountDao.queryPageViewCountList(ZStatisticsPageViewCount.class,params);
    }
}
